'use strict'
myApp
.controller("userCtrl", ["$scope", "$state", "$http", function($scope, $state, $http){
	$http({
		method: "get",
		url: "http://127.0.0.1:8008/info"
	}).success(function(data){
		if(data){
			$scope.user = data;
		}
		if(data == 'error'){
			$state.go("/login");
		}
	});
	$scope.save = function(user){
		$http({
			method: "post",
			url: "http://127.0.0.1:8008/info",
			data: user,
			headers: {
				"Content-Type": "application/x-www-form-urlencoded"
			}
		}).success(function(data){
			alert("保存成功！");
			$state.reload();
		}).error(function(data){
			console.log(data);
		});
	}
}])
.controller("userPasswdCtrl", ["$scope", "$state", "$http", function($scope, $state, $http){
	$http({
		method: "get",
		url: "http://127.0.0.1:8008/passwd"
	}).success(function(data){
		if(data == "error"){
			$state.go("/login");
		}else if(data = "success"){
			$state.go("/.passwd")
		}
	}).error(function(data){
		$state.go("/login")
	});
	$scope.save = function(user){
		$http({
			method: "post",
			url: "http://127.0.0.1:8008/passwd",
			headers: {
				"Content-Type": "application/x-www-form-urlencoded"
			},
			data: user
			
		}).success(function(data){
			if(data == "1"){
				alert("修改成功！");
				$state.reload();
			}else if(data == "error"){
				alert("原始密码有误！")
				$state.reload();
			}else if(data == "login_error"){
				$state.go("/login");
			}
		});
	}
}])
.controller("uploaderCtrl", ["$scope", "$state", "$http", "FileUploader", function($scope, $state, $http, FileUploader){
	$http({
		method: 'get',
		url: 'http://127.0.0.1:8008/headImg'
	}).success(function(data){
		if(data){
			$scope.ip = 'http://127.0.0.1';
			$scope.head_img = data;
		}else if(data = "login_error"){
			$state.go("/login")
		}else{
			$scope.ip = 'http://127.0.0.1';
			$scope.head_img = 'ico_defalut.png';
		}
	});

	var uploader = $scope.uploader = new FileUploader({
		url: "http://127.0.0.1:8008/headImg"
	});
	uploader.filters.push({
            name: 'imageFilter',
            fn: function(item /*{File|FileLikeObject}*/, options) {
                var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
                return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
            }
        });
	uploader.onSuccessItem = function(fileItem, response, status, headers){
		if(fileItem['isSuccess']){
			$state.reload();
			alert('上传成功！');
		}
	}		
}]);

